/*******************************************************************************
 * @brief: systask
 * @Author: aple
 * @Date: 2022-12-04 10:37:03
 * @LastEditors: aple
 * @LastEditTime: 2022-12-07 19:27:41
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "bsp_api.h"
#include "sys_task.h"
#include "framework.h"

/*****************************************************************************
 * @brief   order
 *****************************************************************************/

/*****************************************************************************
 * @brief   var
 *****************************************************************************/

/*****************************************************************************
 * @brief   fun
 *****************************************************************************/

/*******************************************************************************
 * @brief: flash opt
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int flash_opt(int argc, char *argv[])
{
	int cnt = 1;
	uu08 buff[64];
	
	if(argc > 1)
		cnt = atoi(argv[1]);

	dbg_print("NOR SR1: 0x%08X\n", bsp_snor_reg_get(1));
	dbg_print("NOR SR2: 0x%08X\n", bsp_snor_reg_get(2));
	
	// 填充数据
	loop(sizeof(buff))
		buff[index] = 2*index;
	
	// 写数据
	bsp_snor_sector_erase(0);
	bsp_snor_write(0, sizeof(buff), buff);
	mem_u8_disp("H:WRITE:\n", buff, sizeof(buff), 16);
	
	// 读数据
	memset(buff, 0, sizeof(buff));
	bsp_snor_read(0, sizeof(buff), buff);
	mem_u8_disp("H:READ:\n", buff, sizeof(buff), 16);
	
	dbg_puts( SYMBOL_LINE_EQ PTCR);
	
	return 0;
}
DBG_FUN_EXPORT(flash_opt);


/*******************************************************************************
 * @brief: flash write
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int flash_write(int argc, char *argv[])
{
	int addr = 0, data = 0;
	uu08 buff[64];
	
	if(argc > 1)
		addr = atoi(argv[1]) * 4096;
	data = addr/4096;
	if(argc > 2)
		data = atoi(argv[2]);

	// 填充数据
	loop(sizeof(buff))
		buff[index] = data + index;
	
	// 写数据
	bsp_snor_sector_erase(addr);
	bsp_snor_write(addr, sizeof(buff), buff);
	dbg_print("FLASH WRITE ADDRESS: 0x%08X:\n", addr);
	mem_u8_disp("", buff, sizeof(buff), 16);
	
	dbg_puts( SYMBOL_LINE_EQ PTCR);
	
	return 0;
}
DBG_FUN_EXPORT(flash_write);

/*******************************************************************************
 * @brief: flash read
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int flash_read(int argc, char *argv[])
{
	int addr = 0;
	uu08 buff[64];
	
	if(argc > 1)
		addr = atoi(argv[1]) * 4096;

	// 读数据
	memset(buff, addr, sizeof(buff));
	bsp_snor_read(addr, sizeof(buff), buff);
	mem_u8_disp("H:READ:\n", buff, sizeof(buff), 16);
	
	dbg_puts( SYMBOL_LINE_EQ PTCR);
	
	return 0;
}
DBG_FUN_EXPORT(flash_read);

